// pages/market/market.js 
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navIndex:0,
    nav:['全部产品','推荐产品'],
    goods:[
      {
        id:"0",
        img:'http://www.meiliancheng.cn/public/meiliancheng/imagezy/hgj/banner.png',
        title:'皮诺客魔睛智能名片',
        recommend:false,
      },
      {
        id: "0",
        img: 'http://www.meiliancheng.cn/public/meiliancheng/imagezy/hgj/banner.png',
        title: '皮诺客魔睛智能名片',
        recommend: true,
      },
      {
        id: "0",
        img: 'http://www.meiliancheng.cn/public/meiliancheng/imagezy/hgj/banner.png',
        title: '皮诺客魔睛智能名片',
        recommend: false,
      },
      {
        id: "0",
        img: 'http://www.meiliancheng.cn/public/meiliancheng/imagezy/hgj/banner.png',
        title: '皮诺客魔睛智能名片',
        recommend: false,
      },
    ]
  },
  navChoose:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.setData({
      navIndex:index,
    })
  },
  recommend:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var id= e.currentTarget.dataset.id;
    var goods = that.data.goods;
    var recommend = goods[index].recommend;
    if(recommend == true){
     wx.showActionSheet({
       itemList: ['取消推荐'],
       success(res) {
         if(res.tapIndex == 0){
           goods[index].recommend = false;
           that.setData({
             goods: goods,
           })
         }
         wx.request({
           url: app.siteInfo.managerpath + 'getAllProduct',
           method: "post",
           data: {
             mid: app.siteInfo.mid,
             pid: app.siteInfo.pid,
             action: "del",
             id: id
           },
           success: function (res) {

           }
         })
       },
     })
    }else{
      wx.showActionSheet({
        itemList: ['推荐产品'],
        success(res){
          if (res.tapIndex == 0) {
            goods[index].recommend = true;
            that.setData({
              goods: goods,
            })
          }
          wx.request({
            url: app.siteInfo.managerpath + 'getAllProduct',
            method: "post",
            data: {
              mid: app.siteInfo.mid,
              pid: app.siteInfo.pid,
              action: "add",
              id:id
            },
            success: function (res) {
              
            }
          })
        }
      })
    }
  },
  cancel:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var goods = that.data.goods;
    wx.showActionSheet({
      itemList: ['取消推荐'],
      success(res) {
        if (res.tapIndex == 0) {
          goods[index].recommend = false;
          that.setData({
            goods: goods,
          })
        }
        wx.request({
          url: app.siteInfo.managerpath + 'getAllProduct',
          method: "post",
          data: {
            mid: app.siteInfo.mid,
            pid: app.siteInfo.pid,
            action: "del",
            id: id
          },
          success: function (res) {

          }
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: app.siteInfo.managerpath+'getAllProduct',
      method:"post",
      data:{
        mid:app.siteInfo.mid,
        pid:app.siteInfo.pid,
        action:"look"
      },
      success:function(res){
        that.setData({
          goods:res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    wx.request({
      url: app.siteInfo.siteroot + 'relay',
      method: "post",
      data: {
        uid: app.siteInfo.uid,
        pid: app.siteInfo.pid,
        replaysource: "index"
      },
      success: function (res) { }
    })
    return {
      title: app.siteInfo.sharetitle,
      path: "/pages/index/index?share_card_id=" + app.siteInfo.pid + "&shareid=" + app.siteInfo.uid,
      success: function (a) {
        wx.request({
          url: app.siteInfo.siteroot + 'relay',
          method: "post",
          data: {
            uid: app.siteInfo.uid,
            pid: app.siteInfo.pid
          },
          success: function (res) { }
        })
      },
      fail: function (a) { }
    };
  }
})